package com.qyer.search.sort;

import com.qyer.search.cache.mutable.MutableCaches;
import com.qyer.search.exception.SearchException;
import com.qyer.search.model.SearchParameterEntity;
import com.qyer.search.model.lastminute.VSKU;
import com.qyer.search.model.result.SearchEntity;
import com.qyer.search.utils.ListUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by tangzheng on 16/7/19.
 */
public class VPriceSorter implements SearchSorter{
  private SearchParameterEntity sp;
  private SearchEntity searchEntity;
  private int orderDirection ;
  private int from;
  private int to;

  public VPriceSorter(SearchParameterEntity sp, SearchEntity searchEntity, int orderDirection, int from, int to) {
    this.sp = sp;
    this.searchEntity = searchEntity;
    this.orderDirection = orderDirection;
    this.from = from;
    this.to = to;

  }

  @Override public List<SearchEntity> sort() throws SearchException {
    if(searchEntity == null){
      return null;
    }

    Map<Integer,Long> sales = MutableCaches.getInstance().PRICE.getCacheContent();
    List<SearchEntity> searchEntities = new ArrayList<>(1);
    List<VSKU> skus, marketSkus;
    Map<Long,List<VSKU>> treeSkuMap = null;
    if(orderDirection == 0)
      treeSkuMap = new TreeMap<>();
    if(orderDirection == 1)
      treeSkuMap = new TreeMap<>(new Comparator<Long>() {
        @Override public int compare(Long o1, Long o2) {
          return (o2.intValue()-o1.intValue());
        }
      });
    marketSkus = searchEntity.getMarketSkus();
    Collections.sort(marketSkus);
    skus = searchEntity.getCommonSkus();
    int size = skus.size();
    List<VSKU> unknownList = new ArrayList<>(4);
    VSKU sku;
    int id;
    long sale;

    long t1 = System.currentTimeMillis();

    for(int i = 0; i < size; i++){
      sku = skus.get(i);
      try {
        id = sku.getId();
        sale = sales.get(id);
        if(treeSkuMap.containsKey(sale)){
          treeSkuMap.get(sale).add(sku);
        }else {
          List<VSKU> skuList = new ArrayList<>();
          skuList.add(sku);
          treeSkuMap.put(sale, skuList);
        }
      }catch (Exception e){
        if(sku != null)
          unknownList.add(sku);
      }
    }

    long t2 = System.currentTimeMillis();

    /*for(int i = 0; i<unknownList.size(); i++){
      System.out.println("UNKNOWN:"+unknownList.get(i).getId());
    }*/


    skus = new ArrayList<>(marketSkus);
    Iterator<Map.Entry<Long, List<VSKU>>> iterator = treeSkuMap.entrySet().iterator();
    while (iterator.hasNext()) {
      Map.Entry<Long, List<VSKU>> entry = iterator.next();
      List<VSKU> skuList = entry.getValue();
      skus.addAll(skuList);
    }
    skus.addAll(unknownList);
    int v_size = skus.size();
    long t3 = System.currentTimeMillis();

    //System.out.println("1COST:"+(t2-t1));
    //System.out.println("2COST:"+(t3-t2));
    List<VSKU> list = ListUtils.ToSubList(skus, from, to);

    SearchEntity searchEntity = new SearchEntity(list.size());
    searchEntity.addSkus(list);
    searchEntity.setPackageSize(v_size);
    searchEntities.add(searchEntity);

    return searchEntities;
  }
}

